home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0430.dms / q0430.adf / Patch / chdir.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  2KB  |  118 lines

  1. /* File to implement the "chdir" command of Unix under AmigaDOS: */
  2. /* December 1989 Eric Green, public domain.                      */
  3.  
  4. #include <stdio.h>
  5.  
  6. #include <libraries/dos.h>
  7. #include <libraries/dosextens.h>
  8.  
  9. #ifdef LATTICE
  10. #include <proto/dos.h>
  11. #else
  12. struct FileLock *Lock();
  13. int Examine();
  14. struct FileLock *CurrentDir();
  15. void UnLock();
  16. #endif
  17.  
  18. #include <exec/memory.h>
  19.  
  20. #ifdef LATTICE
  21. #include <proto/exec.h>
  22. #else
  23. struct FileInfoBlock *AllocMem();
  24. void FreeMem();
  25. #endif
  26.  
  27. #include <errno.h>
  28.  
  29. #ifdef MANX
  30. #ifndef ENOTDIR
  31. #define ENOTDIR 11
  32. #endif
  33. #endif
  34.  
  35. #ifdef LATTICE
  36. extern int _OSERR;
  37.  
  38. int chdir(char *);
  39.  
  40. #endif
  41.  
  42. int chdir(newpath)
  43.   char *newpath;
  44. {
  45.     register struct FileLock *newdir;
  46.     register struct FileLock *olddir = 0;
  47.  
  48.     register struct FileInfoBlock *fib;
  49.  
  50.     register int ioerr;  /* used as switch for I/O errors. */
  51.  
  52. #ifdef LATTICE
  53.     _OSERR = 0;  /* clear it here... */
  54. #endif
  55.  
  56.     newdir = Lock(newpath,ACCESS_READ);
  57.  
  58.  
  59. /* we have to check & make sure it's really a directory.
  60.    Else, CurrentDir will blithely let us chdir to it! */
  61.  
  62.     if (newdir) {
  63.         /* test out the type before we do anything: */
  64.         fib = AllocMem(sizeof(struct FileInfoBlock),
  65.                         MEMF_CLEAR|MEMF_PUBLIC);
  66.         ioerr = Examine(newdir,fib);
  67.         if (ioerr==0) {
  68. #ifdef LATTICE
  69.             _OSERR = IoErr();
  70. #endif
  71.             errno=ENOTDIR; /* obviously! Since ACTION_EXAMINE
  72.                              doesn't work. */
  73.         } else {
  74.            if (fib->fib_DirEntryType < 0) {
  75.                 errno=ENOTDIR;
  76.            } else {
  77.             olddir=CurrentDir(newdir);
  78.             if (olddir==0) {
  79. #ifdef LATTICE
  80.                 _OSERR = IoErr();
  81. #endif
  82.                 errno=ENOTDIR; /* true, if moot... */
  83.             }
  84.           }
  85.        }
  86.        FreeMem(fib,sizeof(struct FileInfoBlock));
  87.  
  88.     } else {
  89.         errno = ENOENT;
  90.     }
  91.  
  92.     if (newdir==0 || olddir==0) {
  93. #ifdef TEST
  94.         printf("IoErr=%d\t",_OSERR);
  95. #endif
  96.        if (newdir) UnLock(newdir); /* clear up hanging lock. */
  97.        return -1;
  98.     }
  99.  
  100.     UnLock(olddir);  /* no longer needed? */
  101.     return 0;
  102. }
  103.  
  104. #ifdef TEST
  105. void main(int,char **);
  106.  
  107. void main (argc,argv)
  108.     int argc;
  109.     char **argv;
  110. {
  111.     int result;
  112.  
  113.     result=chdir(argv[1]);
  114.  
  115.     printf("chdir = %d\n",result);
  116. }
  117. #endif
  118.